package com.ctu.gzero.operation
{
	import com.ctu.gzero.edit.SelectionState;
	import com.ctu.gzero.elements.ParaElement;
	import com.ctu.gzero.elements.TxtRun;
	import com.ctu.gzero.event.DomEvent;

	public class DomTextOperation extends DomOperation
	{
		
		private var _text:String;
		
		
		public function DomTextOperation(text:String, selectionState:SelectionState)
		{
			super(selectionState);				
			_text = text;
		}

		public function get text():String
		{
			return _text;
		}
		
		override public function doOperation():SelectionState
		{
			//TODO: use ParaEdit
			var operationState:SelectionState;
			var para:ParaElement;
			var txRun:TxtRun;
			var txRunOffset:int;
			
			if(this.absoluteStart == this.absoluteEnd)
			{
				para = dom.findParaAtPosition(this.absoluteStart);
				txRunOffset = this.absoluteStart - para.absoluteStart;
				txRun = para.findTextLeaf(txRunOffset) as TxtRun;
					
				txRun.replaceText(txRunOffset - txRun.relativeStart, txRunOffset - txRun.relativeStart, this.text);
				txRun.textElement.replaceText(txRunOffset - txRun.relativeStart, txRunOffset - txRun.relativeStart, this.text);
				operationState = new SelectionState(dom, this.absoluteStart+this.text.length, this.absoluteStart+this.text.length);
				//dom.interactionManager.setSelection(operationState);
				
			}
			
			return operationState;
		}
	}
}